﻿using Douglas.Tool.Entity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Text;

namespace Douglas.Tool.Filters
{
    public class ApiResultFilter : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext actionExecutedContext)
        {
            if (actionExecutedContext.Exception == null)
            {   //执行成功 取得由 API 返回的资料
                HttpBaseResult<object> apiResult = new HttpBaseResult<object>();
                ObjectResult result = actionExecutedContext.Result as ObjectResult;
                apiResult.Code = "1";
                if (result != null)
                {   // 重新封装回传格式
                    apiResult.Data = result.Value;
                }
                ObjectResult objectResult = new ObjectResult(apiResult);
                actionExecutedContext.Result = objectResult;
            }
            base.OnActionExecuted(actionExecutedContext);
        }
    }
}
